package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.common.Constants;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.Language;
import com.symone.webcms.model.base.ResultMessage;
import com.symone.webcms.service.LanguageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 *   @description : 语言管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2018-12-05
 */
@RestController
@Api(value="/sysLanguage", description="语言 管理")
@RequestMapping("api/language")
public class LanguageController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(LanguageController.class);

    @Autowired
    public LanguageService languageService;

    /**
     * @description : 获取语言分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-12-05
     */
    @RequiresPermissions("sys:lang:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ResultMessage page(Integer pageSize, Integer pageNum) {
        Wrapper<Language> ew = new EntityWrapper<Language>();
        ew.where("deleted=0");

        //logger.debug("SQL:"+ew.originalSql());

        Page<Language> page = new Page<Language>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        languageService.findPage(page, ew);

        return ResultMessage.success("获取语言的分页列表",page);
    }

    /**
     * @description : 获取所有语言记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-12-05
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value="获取所有语言信息", notes="获取所有语言信息")
    public ResultMessage list() {
        return ResultMessage.success("获取所有语言信息",languageService.findAll());
    }

    /**
     * @description : 通过id获取语言
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-12-05
     */
    @RequiresPermissions("sys:lang:get")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取语言信息成功!",languageService.selectById(id));
    }

    /**
     * @description : 添加语言
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-12-05
     */
    @RequiresPermissions("sys:lang:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultMessage add(Language language){
        languageService.insert(language);
        return ResultMessage.success("添加语言成功!");
    }

    /**
     * @description : 通过id逻辑删除语言
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-12-05
     */
    @RequiresPermissions("sys:lang:remove_flag")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    public ResultMessage removeFlag(Long id){
        if(languageService.deleteFlagById(id)) {
            return ResultMessage.success("逻辑语言删除成功！");
        } else {
            return ResultMessage.fail("逻辑语言删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除语言
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-12-05
     */
    @RequiresPermissions("sys:lang:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    public ResultMessage remove(long id){
        if(languageService.deleteById(id)) {
            return ResultMessage.success("物理删除语言成功！");
        } else {
            return ResultMessage.fail("物理删除语言失败！");
        }
    }

    /**
     * @description : 更新语言信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-12-05
     */
    @RequiresPermissions("sys:lang:edit")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResultMessage update(Language language) {
        if(languageService.updateById(language)) {
            List<Language> languages = (List<Language>) getSession().getAttribute(Constants.SYS_LANGUAGE_LIST);
            languages.add(language);
            getSession().setAttribute(Constants.SYS_LANGUAGE_LIST,languages);
            return ResultMessage.success("语言信息更新成功!");
        }else{
            return ResultMessage.fail("语言信息更新失败!");
        }
    }

    /**
    * @description : 清除语言记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2018-12-05
    */
    @RequiresPermissions("sys:lang:clear")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    public ResultMessage clear(){
        int num = languageService.clear();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有语言记录被清除！");
        }
    }



}
